﻿// -------------------------------------------------------------------------
//    @FileName         :    NFCityWorldObjectFunc.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-09-17
//    @Email			:    445267987@qq.com
//    @Module           :    NFCityWorldObjectFunc
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFCityWorldObjectFunc.h"

#include "NFCityMapUtil.h"
#include "NFCityPacketSendUtil.h"
#include "NFCityPlayer.h"

NFMakeNpcView::NFMakeNpcView(NFCityPlayer* pPlayer, proto_ff::Proto_CSRsp* pCSRsp) : m_iEnterViewCnt(0), m_pPlayer(pPlayer), m_pCSRsp(pCSRsp)
{
    m_pCSRsp->mutable_map_enter_view_ntf()->set_reason(proto_ff::ENTER_VIEW_ALL);
    if (pPlayer)
    {
        m_pCSRsp->mutable_map_enter_view_ntf()->set_map_id(pPlayer->GetMapDescId());
    }
}

int NFMakeNpcView::DoFunc(NFCityWorldObject* pWorldObject)
{
    CHECK_NULL(0, pWorldObject);

    if (m_pPlayer && !pWorldObject->IsPlayer() && pWorldObject->IsVisible(m_pPlayer))
    {
        NFCityMapUtil::MakeEnterView(pWorldObject, m_pCSRsp->mutable_map_enter_view_ntf());
        ++m_iEnterViewCnt;

        //syn action
        NFCityMapUtil::SynAction(pWorldObject, m_pPlayer);
    }
    return 0;
}

int NFMakeNpcView::Send()
{
    if (m_pPlayer && m_pPlayer->IsEnterSvr() && m_pCSRsp->mutable_map_enter_view_ntf()->view_list_size() > 0)
    {
        NFCityPacketSendUtil::SendToClient(m_pPlayer, proto_ff::E_CS_MAP_ENTERVIEW_NTF, m_pCSRsp);
    }
    return 0;
}

NFMakePlayerView::NFMakePlayerView(NFCityPlayer* pPlayer, proto_ff::Proto_CSRsp* pCSRsp) : m_iEnterViewCnt(0), m_pPlayer(pPlayer), m_pCSRsp(pCSRsp)
{
    m_pCSRsp->mutable_map_enter_view_ntf()->set_reason(proto_ff::ENTER_VIEW_ALL);
    if (pPlayer)
    {
        m_pCSRsp->mutable_map_enter_view_ntf()->set_map_id(pPlayer->GetMapDescId());
    }
}

int NFMakePlayerView::DoFunc(NFCityWorldObject* pWorldObject)
{
    CHECK_NULL(0, pWorldObject);

    if (m_pPlayer && pWorldObject->IsPlayer() && pWorldObject->IsVisible(m_pPlayer))
    {
        NFCityMapUtil::MakeEnterView(pWorldObject, m_pCSRsp->mutable_map_enter_view_ntf());
        ++m_iEnterViewCnt;

        if (!m_apPlayer.full())
        {
            auto pOtherPlayer = dynamic_cast<NFCityPlayer*>(pWorldObject);
            if (pOtherPlayer)
            {
                m_apPlayer.push_back(pOtherPlayer);
            }
        }
    }

    return 0;
}

int NFMakePlayerView::Send()
{
    if (m_pPlayer && m_pPlayer->IsEnterSvr() && m_pCSRsp->mutable_map_enter_view_ntf()->view_list_size() > 0)
    {
        NFCityPacketSendUtil::SendToClient(m_pPlayer, proto_ff::E_CS_MAP_ENTERVIEW_NTF, m_pCSRsp);
    }

    for (int i = 0; i < m_apPlayer.size(); ++i)
    {
        //syn action
        NFCityMapUtil::SynAction(m_apPlayer[i], m_pPlayer);
    }

    return 0;
}

NFOtherEnterView::NFOtherEnterView(NFCityPlayer* pPlayer) : m_iEnterViewCnt(0), m_pPlayer(pPlayer)
{
    m_oCSRsp.mutable_map_enter_view_ntf()->set_reason(proto_ff::ENTER_VIEW_ALL);
    if (pPlayer)
    {
        m_oCSRsp.mutable_map_enter_view_ntf()->set_map_id(pPlayer->GetMapDescId());
    }
}

int NFOtherEnterView::DoFunc(NFCityWorldObject* pWorldObject)
{
    CHECK_NULL(0, pWorldObject);

    if (m_pPlayer && pWorldObject != m_pPlayer && pWorldObject->IsVisible(m_pPlayer))
    {
        NFCityMapUtil::MakeEnterView(pWorldObject, m_oCSRsp.mutable_map_enter_view_ntf());
        ++m_iEnterViewCnt;

        //syn action
        NFCityMapUtil::SynAction(pWorldObject, m_pPlayer);
    }

    return 0;
}

int NFOtherEnterView::Send()
{
    if (m_pPlayer && m_pPlayer->IsEnterSvr() && m_iEnterViewCnt > 0)
    {
        NFCityPacketSendUtil::SendToClient(m_pPlayer, proto_ff::E_CS_MAP_ENTERVIEW_NTF, &m_oCSRsp);
    }

    return 0;
}

NFEnterViewOther::NFEnterViewOther(NFCityWorldObject* pWorldObject)
{
    m_pActiveObject = pWorldObject;

    m_oCSRsp.mutable_map_enter_view_ntf()->set_reason(proto_ff::ENTER_VIEW_ALL);
    if (pWorldObject)
    {
        m_oCSRsp.mutable_map_enter_view_ntf()->set_map_id(pWorldObject->GetMapDescId());
    }

    //make view
    NFCityMapUtil::MakeEnterView(pWorldObject, m_oCSRsp.mutable_map_enter_view_ntf());

    m_bIsAction = false;
    NFCityMapUtil::MakeLastAction(pWorldObject, m_bIsAction, m_oCSActionRsp.mutable_map_action_ntf());
}

int NFEnterViewOther::DoFunc(NFCityWorldObject* pWorldObject)
{
    CHECK_NULL(0, pWorldObject);

    if (pWorldObject && pWorldObject->IsPlayer() && pWorldObject != m_pActiveObject && m_pActiveObject->IsVisible(pWorldObject))
    {
        NFCityPlayer* pPlayer = dynamic_cast<NFCityPlayer*>(pWorldObject);
        if (!pPlayer->IsEnterSvr())
        {
            return 0;
        }

        NFCityPacketSendUtil::SendToClient(pPlayer, proto_ff::E_CS_MAP_ENTERVIEW_NTF, &m_oCSRsp);

        if (m_bIsAction)
        {
            NFCityPacketSendUtil::SendToClient(pPlayer, proto_ff::E_CS_MAP_ACTION_NTF, &m_oCSActionRsp);
        }
    }

    return 0;
}

NFRefreshViewOther::NFRefreshViewOther(NFCityWorldObject* pWorldObject)
{
    m_pActiveObject = pWorldObject;

    m_oCSRsp.mutable_map_refresh_view_ntf()->set_reason(proto_ff::ENTER_VIEW_ALL);
    if (pWorldObject)
    {
        m_oCSRsp.mutable_map_refresh_view_ntf()->set_map_id(pWorldObject->GetMapDescId());
    }

    //make view
    NFCityMapUtil::MakeRefreshView(pWorldObject, m_oCSRsp.mutable_map_refresh_view_ntf());
}


int NFRefreshViewOther::DoFunc(NFCityWorldObject* pWorldObject)
{
    CHECK_NULL(0, pWorldObject);

    //reflesh self view
    if (pWorldObject->IsPlayer() && pWorldObject == m_pActiveObject)
    {
        auto pPlayer = dynamic_cast<NFCityPlayer*>(pWorldObject);
        CHECK_NULL(0, pPlayer);
        if (!pPlayer->IsEnterSvr() || pPlayer->GetMapInstId() != m_pActiveObject->GetMapInstId())
        {
            return 0;
        }

        NFCityPacketSendUtil::SendToClient(pPlayer, proto_ff::E_CS_MAP_REFRESH_VIEW_NTF, &m_oCSRsp);
    }

    if (pWorldObject && pWorldObject->IsPlayer() && pWorldObject != m_pActiveObject && m_pActiveObject->IsVisible(pWorldObject))
    {
        auto pPlayer = dynamic_cast<NFCityPlayer*>(pWorldObject);
        CHECK_NULL(0, pPlayer);
        if (!pPlayer->IsEnterSvr() || pPlayer->GetMapInstId() != m_pActiveObject->GetMapInstId())
        {
            return 0;
        }

        NFCityPacketSendUtil::SendToClient(pPlayer, proto_ff::E_CS_MAP_REFRESH_VIEW_NTF, &m_oCSRsp);
    }

    return 0;
}

int NFOtherLeaveView::DoFunc(NFCityWorldObject* pWorldObject)
{
    CHECK_NULL(0, pWorldObject);

    if (pWorldObject != m_pPlayer)
    {
        NFCityMapUtil::MakeLeaveView(pWorldObject, m_oCSRsp.mutable_map_leave_view_ntf());
        ++m_iLeaveViewCnt;
    }

    return 0;
}

int NFOtherLeaveView::Send()
{
    if (m_pPlayer && m_pPlayer->IsEnterSvr() && m_iLeaveViewCnt)
    {
        NFCityPacketSendUtil::SendToClient(m_pPlayer, proto_ff::E_CS_MAP_LEAVEVIEW_NTF, &m_oCSRsp);
    }

    return 0;
}

NFLeaveViewOther::NFLeaveViewOther(NFCityWorldObject* pWorldObject)
{
    m_pActiveObject = pWorldObject;

    m_oCSRsp.mutable_map_leave_view_ntf()->set_reason(proto_ff::LEAVE_VIEW_OUT);

    //make leave view
    NFCityMapUtil::MakeLeaveView(pWorldObject, m_oCSRsp.mutable_map_leave_view_ntf());
}

int NFLeaveViewOther::DoFunc(NFCityWorldObject* pWorldObject)
{
    if (pWorldObject && pWorldObject->IsPlayer() && pWorldObject != m_pActiveObject)
    {
        NFCityPlayer* pPlayer = dynamic_cast<NFCityPlayer*>(pWorldObject);
        CHECK_NULL(0, pPlayer);
        if (!pPlayer->IsEnterSvr())
        {
            return 0;
        }

        NFCityPacketSendUtil::SendToClient(pPlayer, proto_ff::E_CS_MAP_LEAVEVIEW_NTF, &m_oCSRsp);
    }

    return 0;
}

int NFWorldObjectNotifyMessage::DoFunc(NFCityWorldObject* pWorldObject)
{
    CHECK_NULL(0, pWorldObject);

    if (pWorldObject->IsPlayer() && pWorldObject != m_pActiveObject)
    {
        NFCityPlayer* pPlayer = dynamic_cast<NFCityPlayer*>(pWorldObject);
        CHECK_NULL(0, pPlayer);
        if (!pPlayer->IsEnterSvr())
        {
            return 0;
        }

        if (m_pActiveObject && pPlayer->GetMapInstId() != m_pActiveObject->GetMapInstId())
        {
            LOG_ERR(pPlayer->GetRoleID(), -1, "player is not in same map! pPlayer->GetMapInstId():{} m_pActiveObject->GetMapInstId():{}", pPlayer->GetMapInstId(), m_pActiveObject->GetMapInstId());
            return 0;
        }

        NFCityPacketSendUtil::SendToClient(pPlayer, m_iCmd, m_pCSRsp);

        return 0;
    }
    return 0;
}

int NFWorldObjectNotifyMessage::Send()
{
    return 0;
}
